<?php

/**
 * This is the model class for table "sd.category_property".
 *
 * The followings are the available columns in table 'sd.category_property':
 * @property string $id
 * @property string $category_id
 * @property string $property_id
 *
 * The followings are the available model relations:
 * @property Categories $category
 * @property Property $property
 */
class CategoryProperty extends CBaseActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sd.category_property';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, property_id', 'required'),
			array('category_id, property_id', 'length', 'max'=>10),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, category_id, property_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'Categories', 'category_id'),
			'property' => array(self::BELONGS_TO, 'Property', 'property_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_id' => 'Category',
			'property_id' => 'Property',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('category_id',$this->category_id,true);
		$criteria->compare('property_id',$this->property_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return CategoryProperty the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function getAll($categoryId)
	{
		if($categoryId < 1)
			return array();

		$criteria=new CDbCriteria;

		$criteria->with = 'property';
		$criteria->select = 'categoty_property.*';
		$criteria->addInCondition('category_id', (array)$categoryId);

		$oRowset = $this->findAll($criteria);

		return $oRowset;
	}

	public function getLangedRow($id)
	{
		$oProperty = new Property();
		$oCriteria = $oProperty->getDbCriteria();

		$oCriteria->join = 'INNER JOIN category_property cp ON cp.property_id = parent_id';
		$oCriteria->addCondition(array('cp.category_id' => $id));

		$oRowset = $oProperty->findAll($oCriteria);

		return $oRowset;
	}

	public function getByCategoryId($id)
	{
		$oProperty = new Property();
		$oCriteria = $oProperty->getDbCriteria();

		$oCriteria->join = 'INNER JOIN category_property cp ON cp.property_id = t.id';
		$oCriteria->order = "cp.category_id ASC";

		$oCriteria->addInCondition('cp.category_id', (array)$id);


		$oRowset = $oProperty->findAll($oCriteria);

		return $oRowset;
	}

	public function getByType($id, $type = '3')
	{
		if(!$id)
			return array();

		$aRes = Yii::app()->db->createCommand()
			->select('*')
			->from('property p')
			->join('category_property cp', 'p.id = cp.property_id')
			->join('property_value pv', 'pv.property_id = p.id')
			->group('pv.id')
			->where('cp.category_id IN('. implode(', ', (array)$id) .') AND p.type = :type', array(
			                                                     ':type' => $type
			                                                ))
			->queryAll();

		return $aRes;
		print_r($aRes); exit;

	}

}
